<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {
	protected $data;
	protected $img_config;
	function __construct() {
		parent::__construct();
		$this->load->library('upload');
		$this->load->model('model_product');
		$this->load->model('model_news');
		$this->load->model('model_category');
		$this->load->model('model_admin');
		$this->load->helper('array');
		//$this->output->enable_profiler();
		
		$this->config_image['create_thumb'] = TRUE;
		$this->config_image['maintain_ratio'] = TRUE;
		$this->config_image['width'] = 300;
		$this->config_image['height'] = 130;
		$this->load->library('image_lib');
		if(!$this->session->userdata('user') && 'login' != $this->router->fetch_method()){
			redirect('admin/login');
		}
	}
	
	function login()
	{
		if($this->input->post('username')){
			$data['username'] = $this->input->post('username');
			$data['password'] = $this->input->post('password');
			$data['password'] = md5($data['password']);
			$user = $this->model_admin->login($data);
			if(!empty($user)){
				$this->session->set_userdata('user', $user['username']);
				redirect('admin');
			}
		}
		$this->load->view('admin/login_tpl');
	}

	function logout(){
		$this->session->unset_userdata('user');
		redirect('admin/login');
	}
	
	function index()
	{
		$this->_render('admin_index_tpl');
		
	}
	
	function product()
	{
		$this->data['products'] = $this->model_product->get_all();
		$this->_render('list_product_tpl');
	}
	
	function edit_product($id = false){
        $this->data['product']=array();
		if(!empty($id)){
			$this->data['product'] = $this->model_product->get($id);
		}
		$this->data['categories'] = $this->model_category->get_all();
		$this->_render('edit_product_tpl');
	}
	
	function news()
	{
		$this->data['news'] = $this->model_news->get_all();
		$this->_render('list_news_tpl');
	}
	
	function edit_news($id = false){
        $this->data['news']=array();
		if(!empty($id)){
			$this->data['news'] = $this->model_news->get($id);
		}
		$this->_render('edit_news_tpl');
	}
	
	function home_highlight(){
        $this->data['products'] = $this->model_product->get_home_highlight();
		$this->_render('home_highlight_tpl');
	}
	
	function product_highlight(){
        $this->data['products'] = $this->model_product->get_product_highlight();
		$this->_render('product_highlight_tpl');
	}
	
	function edit_home_highlight($id = ''){
        $this->data['product']=array();
        $this->data['products']=array();
		if(!empty($id)){
			$this->data['product'] = $this->model_product->get_home_highlight($id);
		}else{
			$this->data['products'] = $this->model_product->get_home_highlight();
		}
		$this->_render('edit_home_highlight_tpl');
	}
	
	function edit_product_highlight($id = ''){
        $this->data['product']=array();
        $this->data['products']=array();
		if(!empty($id)){
			$this->data['product'] = $this->model_product->get_product_highlight($id);
		}else{
			$this->data['products'] = $this->model_product->get_product_highlight();
		}
		$this->_render('edit_product_highlight_tpl');
	}
	
	function save_home_highlight(){
		$id = '';
		if($this->input->post('save_home_highlight')){
			$data['id'] = $this->input->post('id', true);
			$data['title'] = $this->input->post('title', true);
			$data['title_color'] = $this->input->post('title_color', true);
			$data['link'] = $this->input->post('link', true);

			if($file_name = $this->_upload_image('pic')){
				$data['pic'] = $file_name;
			}
			
			if(!empty($data['title']) || !empty($data['link']) || !empty($data['pic'])){
				$id = $this->model_product->save_home_highlight($data);
			}
		}
		redirect('/admin/edit_home_highlight/'.$id);
	}
	
	function save_product_highlight(){
		$id = '';
		if($this->input->post('save_product_highlight')){
			$data['id'] = $this->input->post('id', true);
			$data['title'] = $this->input->post('title', true);
			$data['link'] = $this->input->post('link', true);

			if($file_name = $this->_upload_image('pic')){
				$data['pic'] = $file_name;
			}
			
			if(!empty($data['title']) || !empty($data['link']) || !empty($data['pic'])){
				$id = $this->model_product->save_product_highlight($data);
			}
		}
		redirect('/admin/edit_product_highlight/'.$id);
	}
	
	function delete_product($id='')
	{
		if(empty($id)){
			exit('true');
		}
		$this->model_product->delete($id);
		echo 'true';
	}
	
	function delete_home_highlight($id='')
	{
		if(empty($id)){
			exit('true');
		}
		$this->model_product->delete_home_highlight($id);
		echo 'true';
	}
	
	function delete_product_highlight($id='')
	{
		if(empty($id)){
			exit('true');
		}
		$this->model_product->delete_product_highlight($id);
		echo 'true';
	}
	
	function delete_news($id='')
	{
		if(empty($id)){
			exit('true');
		}
		$this->model_news->delete($id);
		echo 'true';
	}

	function save_product()
	{
		$data['id'] = $this->input->post('id', true);
		$data['title'] = $this->input->post('title', true);
		$data['slug'] = $this->input->post('slug', true);
		$data['category_id'] = $this->input->post('category_id', true);
		$data['description'] = $this->input->post('description', true);
		$data['excerpt'] = $this->input->post('excerpt', true);
		$data['quote'] = $this->input->post('quote', true);
		
		if(empty($data['title'])){
			$error['title_error'] = true;
		}
		if(empty($data['description'])){
			$error['description_error'] = true;
		}
		if(empty($data['excerpt'])){
			$error['excerpt_error'] = true;
		}

		if(!empty($data['slug'])){
			$data['slug'] = url_title($data['slug'], '-', TRUE);
		}else{
			$data['slug'] = url_title($data['title'], '-', TRUE);
		}

		for($i=1; $i<7; ++$i){
			if(empty($_FILES['pic'.$i]['name'])){
				continue;
			}
			if($file_name = $this->_upload_image('pic'.$i, true)){
				$data['pic'.$i] = $file_name;
			}
		}
		
		if(empty($error)){
			$product_id = $this->model_product->save($data);
			redirect('/admin/edit_product/'.$product_id);
		}else{
			$this->data['error'] = $error;
		}
		$this->data['product'] = $data;
	}
	
	function save_news()
	{
		$data['id'] = $this->input->post('id', true);
		$data['title'] = $this->input->post('title', true);
		$data['slug'] = $this->input->post('slug', true);
		$data['description'] = $this->input->post('description', true);
		/* $data['category_id'] = $this->input->post('category_id', true);
		$data['excerpt'] = $this->input->post('excerpt', true);
		$data['quote'] = $this->input->post('quote', true); */
		
		if(empty($data['title'])){
			$error['title_error'] = true;
		}
		if(empty($data['description'])){
			$error['description_error'] = true;
		}

		if(!empty($data['slug'])){
			$data['slug'] = url_title($data['slug'], '-', TRUE);
		}else{
			$data['slug'] = url_title($data['title'], '-', TRUE);
		}

		if($file_name = $this->_upload_image('pic1', true)){
			$data['pic1'] = $file_name;
		}
		
		if(empty($error)){
			$id = $this->model_news->save($data);
			redirect('/admin/edit_news/'.$id);
		}else{
			$this->data['error'] = $error;
		}
		$this->data['news'] = $data;
	}
	
	function _upload_image($fieldname = '', $create_thumb = true){
		if($this->upload->do_upload($fieldname)){
			$data = $this->upload->data();
			if($create_thumb){
				$this->config_image['source_image'] = $data['full_path'];
				$this->image_lib->initialize($this->config_image);
				if (!$this->image_lib->resize())				
				{					
					//echo $this->image_lib->display_errors();				
				}
			}
			return $data['file_name'];
		}else{
			//echo $this->upload->display_errors();
		}
		return false;
	}
	
	function _render($tpl='', $return = false)
	{
		$content = $this->load->view('admin/header_tpl', null, true);
		$content .= $this->load->view('admin/'.$tpl, $this->data, true);
		$content .= $this->load->view('admin/footer_tpl', null, true);
		if ($return) {
			return $content;
		}
		echo $content;
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */